﻿/*

Copyright (c) 2009, Stephen Remde
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY STEPHEN REMDE ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL STEPHEN REMDE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Graphs;
using System.Windows.Threading;

namespace ConceptLink
{
    public partial class Pareto : UserControl, Node.INodeCallbacks
    {
        public Pareto()
        {
            
            InitializeComponent();
        }

        private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var width = e.NewSize.Width;
            var height = e.NewSize.Height;

            float s = 5;
            Arrows.Children.Clear();

            Arrows.Children.Add(new Line() { X1 = 0, Y1 = 0, X2 = 0, Y2 = height, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });
            Arrows.Children.Add(new Line() { X1 = 0, Y1 = 0, X2 = s, Y2 = s, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });
            Arrows.Children.Add(new Line() { X1 = 0, Y1 = 0, X2 = -s, Y2 = s, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });

            Arrows.Children.Add(new Line() { X1 = width, Y1 = height, X2 = 0, Y2 = height, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });
            Arrows.Children.Add(new Line() { X1 = width, Y1 = height, X2 = width - s, Y2 = height + s, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });
            Arrows.Children.Add(new Line() { X1 = width, Y1 = height, X2 = width - s, Y2 = height - s, StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) });
        }

        public class ParetoArticleNode : Node
        {
            public Article article;
            public float relX;
            public float relY;
        }

        public List<ParetoArticleNode> Nodes;
        
        public void Show(Article x, Article y, Article[] nodes)
        {
            XAxisTitle.Text = "Relatedness to '" + x.title + "'";
            YAxisTitle.Text = "Relatedness to '" + y.title + "'";

            PlotArea.Children.Clear();

            Loading.Visibility = Visibility.Visible;
            
            Nodes = new List<ParetoArticleNode>();

            foreach (var art in nodes)
            {
                ParetoArticleNode p = new ParetoArticleNode() { article = art, text = art.title, visible = true, id = art.id, node_radius = 8 };        
                Nodes.Add(p);
            }

            var tosearch = nodes.Select(n => n.title).ToArray();

            Relatedness.MassGet(x.title, tosearch, delegate
            {
                Relatedness.MassGet(y.title, tosearch, delegate
                {
                    foreach(var n in Nodes)
                    {
                        float relatedness;
                        
                        if (Relatedness.TryGet(x.title, n.text, out relatedness))
                        {
                            n.relX = relatedness;
                            
                            if (Relatedness.TryGet(y.title, n.text, out relatedness))
                            {
                        
                                n.relY = relatedness;
                                
                                if (n.relX > 0 && n.relY > 0)
                                    n.UpdateUIElemet(this);
                            }
                        }
                    }

                    Loading.Visibility = Visibility.Collapsed;
                    
                    this.Dispatcher.BeginInvoke( delegate {
                        this.LayoutRoot.UpdateLayout();
                        PlotArea_SizeChanged(null, null);
                    });

                    
                });
            });               
        }

        public void HideLabels()
        {
            foreach (var n in Nodes)
                if (n.TextBlock != null)
                    n.TextBlock.Visibility = Visibility.Collapsed;

        }

        public void ShowLabels()
        {
            foreach (var n in Nodes)
                if (n.TextBlock != null)
                    n.TextBlock.Visibility = Visibility.Visible;
        }





        #region node click handeling


        public delegate void NodeEvent(Pareto sender, Node n);

        public event NodeEvent NodeClicked;
        public event NodeEvent NodeHover;
        public event NodeEvent NodeLeave;

        #region INodeCallbacks Members


        Canvas Node.INodeCallbacks.CanvasToAddTo
        {
            get { return PlotArea; }
        }

        void Node.INodeCallbacks.NodeClickedCallback(Node n)
        {
            if (NodeClicked != null) NodeClicked(this, n);
        }

        void Node.INodeCallbacks.NodeHoverCallback(Node n)
        {
            if (NodeHover != null) NodeHover(this, n);
        }

        void Node.INodeCallbacks.NodeLeaveCallback(Node n)
        {
            if (NodeLeave != null) NodeLeave(this, n);
        }

#endregion

    
#endregion

        protected override Size MeasureOverride(Size availableSize)
        {
            var s = base.MeasureOverride(availableSize);
            var min = Math.Min(s.Height, s.Width);
            return new Size(min, min);
        }
      

        private void PlotArea_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var n in Nodes)
            {
                n.x = n.relX * (float)PlotArea.ActualWidth;
                n.y = (float)PlotArea.ActualHeight - n.relY * (float)PlotArea.ActualHeight;
               if (n.relX > 0 && n.relY > 0)
                    n.UpdateUIElemet(this);
            }
        }


        public void ShowLabel(Node n)
        {
            if (n.TextBlock != null)
                n.TextBlock.Visibility = Visibility.Visible;
        }

        public void ResetView()
        {
            ShowLabels();
            foreach (var no in Nodes)
            {
                no.color = Node.default_color;
                no.node_radius = 8;
                no.UpdateUIElemet();
            }
        }
        int zorder = 10;
        public void BringToTop(Node np)
        {
            if (np.grid != null)
                Canvas.SetZIndex(np.grid, zorder++);
        }

        private void YAxisTitle_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var t = ((YAxisTitle.RenderTransform as TransformGroup).Children[0] as TranslateTransform);
            t.X = -e.NewSize.Height / 2;
            t.Y = -e.NewSize.Width / 2;

        }

    }
}
